來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)


問題描述

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

請考慮以下情況。

你有一個存儲過程返回多個結果集,用於為報告目的運行的查詢。

proc 還使用表值參數(SQL 2008)。http://www.sommarskog.se/arrays‑in‑sql‑2008 .html#LINQ_EF

這些結果集是只讀的,這意味著您不必擔心更新、刪除或插入。

這些結果集不映射到數據庫中的任何表;這些是報告結果,而不是數據庫表的鏡像。

現在,我的背景是 asp 經典,雖然我一直在嘗試閱讀所有 .NET 數據訪問策略,但似乎:(1) MS 正在推動實體框架 (2) MS 有過多的數據訪問/架構策略 (3) 整個主題是一個持續爭論的主題 (4) 項目的情況有助於決定正確的數據訪問策略,以及ORM 似乎沒有針對這種數據訪問場景進行優化 (5) 沒有用於映射多個結果集存儲過程的開箱即用解決方案

另外,我正在尋找對 html 的完全控制輸出,所以如果使用後面的代碼,只有中繼器和文字將用於數據綁定。

我發現 Web Forms 編程模型無法提供代碼和內容的完全分離,並且可以像 asp classic 一樣“意大利麵條”。(MVC的方式看起來更像asp classic,但是這個項目已經是WebForms了。)

我認為通過在同一頁面上進行數據訪問和內聯腳本 <% %> 來實現這一點相當容易。以下是可能產生的代碼示例:

<%
' data access (skipping a bunch of code)....
Dim myDataSet As New DataSet()
myCommand.Fill(myDataSet)
'...etc.    

Teachers = myDataSet.Tables(0).Rows
Students = myDataSet.Tables(1).Rows
'... etc.%>

然後

<%  If Teachers.Count > 0 Then%>
<table><%  For Each _Teachers In Teachers%>
    <tr>
        <td><%= _Teachers(0)%></td>
        <td><%= _Teachers(1)%></td>
    </tr><% Next %>    
</table>
<%  Else%>Hey, there's no records.<% End If %> 

(當我嘗試在“受保護的子頁面加載...”下的代碼中分離數據訪問時,我在 .aspx 頁面中使用的代碼中的變量不斷給出錯誤:“[variable] is未聲明。由於其保護級別,它可能無法訪問。")

如果來自 proc 的結果集不是強類型的,那是世界末日嗎?糟糕的編程實踐,可維護性的噩夢,最糟糕的選擇,等等?

如果存儲過程中的結果集應該是強類型的,那麼最有效的方法是什麼?(我找不到關於這個主題的簡單教程。)

在此先感謝您的幫助。


參考解法

方法 1:

It sounds like you're asking whether you should continue to use ADO.NET containers or custom domain classes for your data.

If you feel you need the strong typing of a class, and some validation of input, or other logic applied to your data before display, then consider writing a method to convert a DataTable into a collection of YourClass. Otherwise, if this is for display, then there's nothing wrong with sticking with the DataTable. If you're providing an interface for other components, or need the features that a class would give you, go for the strong typing of a class.

public IEnumerable<Teacher> ConvertToTeachers(DataTable dt)
{
    foreach (var row in dt.Rows.AsEnumerable())
    {
        //create a teacher from this row. modify row indexers as required.
        yield return new Teacher{ TeacherName = row["Name"].Value,
                                  Location = row["Location"].Value };
    }   
}

On the presentation tier, consider leveraging the server controls that ASP.NET webforms provides you. They provide data‑binding capabilities that remove all that looping.

  • Ensure your gridview is designed as you like. Proper columns containing the data you want: regular bound fields and hyperlinks.

  • bind your data to the grid.

 gridViewTeachers.DataSource = myDataSet.Tables(0).Rows
 gridViewTeachers.DataBind

(by mg1075p.campbell)

參考文件

  1. asp.net multiple result set from proc: is it necessary to map results to class? If so, how? (CC BY‑SA 3.0/4.0)

#ASP.NET #ado.net #data-access-layer






相關問題

System.Reflection.Assembly.LoadFile 鎖定文件 (System.Reflection.Assembly.LoadFile Locks File)

如何在沒有全局變量的情況下一直保留我的變量? (How can I keep my variable all the time without global variables?)

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)

無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

使用 ASP.Net 教初學者 Web 開發的小項目想法 (Small projects ideas to teach beginners web development using ASP.Net)

SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)

企業庫異常處理應用程序塊和日誌記錄應用程序塊在 ASP.NET 中的正確使用 (Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET)

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)







留言討論